/**
 * Copyright © 2009 Qasky. All rights reserved.
 *
 * 整个项目通用状态管理
 *
 * @author Wang Hongya
 * @version 0.0.0 2019/11/20
 *
 * History:
 * (author, date, desc)
 * (Wang Hongya, 20191120, 添加注释)
 */
import router from '@/router'
import Util from '@/libs/util'

const common = {
    state: {
        formStyle: {
            labelCol: {span: 6},
            wrapperCol: {span: 17}
        },
        formStyleT: {
            labelCol: {span: 8},
            wrapperCol: {span: 15}
        },
        formStyleUnit: {
            labelCol: {span: 8},
            wrapperCol: {span: 12},
        },
        formItemLayoutTask: {
            wrapperCol: {span: 20, offset: 6}
        },
        formItemLayoutWithOutLabel: {
            wrapperCol: {span: 20, offset: 8}
        },
        menuList: [],
        rootSubmenuKeys: [],
        netWorkList: [],
        collapsed:true,
        showMainFlag:true,
    },
    mutations: {
        /**
         * Function    : updateMenulist
         * Description : 更新导航状态
         * Input       : state,状态
         * Return      : None
         */
        updateMenulist (state) {
            const accessCode = parseInt(sessionStorage.getItem('roleType'));
            //const accessCode = 5
            const routerAll = router.options.routes[0].children;
            console.log(routerAll)
            const menuList = [];
            let rootSubmenuKeys = [];
            routerAll.forEach((item, index) => {
                if (item.meta.access !== undefined) {
                    if (Util.showThisRoute(item.meta.access, accessCode)) {
                        if (item.children.length === 1) {
                            menuList.push(item);
                        } else {
                            const len = menuList.push(item);
                            let childrenArr = [];
                            childrenArr = item.children.filter(child => {
                                if (child.meta.access !== undefined) {
                                    if (child.meta.access.indexOf(accessCode) >= 0) {
                                        return child;
                                    }
                                } else {
                                    return child;
                                }
                            });
                            menuList[len - 1].children = childrenArr;
                        }
                    }
                } else {
                    if (item.children.length === 1) {
                        menuList.push(item);
                    } else {
                        const len = menuList.push(item);
                        let childrenArr = [];
                        childrenArr = item.children.filter(child => {
                            if (child.meta.access !== undefined) {
                                if (Util.showThisRoute(child.meta.access, accessCode)) {
                                    return child;
                                }
                            } else {
                                return child;
                            }
                        });
                        if (childrenArr === undefined || childrenArr.length === 0) {
                            menuList.splice(len - 1, 1);
                        } else {
                            const handledItem = JSON.parse(JSON.stringify(menuList[len - 1]));
                            handledItem.children = childrenArr;
                            menuList.splice(len - 1, 1, handledItem);
                        }
                    }
                }
            });
            //state.menuList = menuList


            /**
             * 初始化挂载首页的处理
             */
           // menuList.splice(0, 1);
            state.menuList = menuList;
            menuList.forEach(item=>{
                rootSubmenuKeys.push(item.key)
            })
            console.log(rootSubmenuKeys)
            state.rootSubmenuKeys = rootSubmenuKeys
        },
        //获取是否显示主要信息
        getShowMainFlag(state, arg) {
            state.showMainFlag = arg;
        },
        getNetWorkList(state, arg) {
            state.netWorkList = arg;
        },
    },
    actions: {
        setShowMainFlag({ commit }, arg) {
            commit('getShowMainFlag', arg)
        },
        setNetWorkList({ commit }, arg) {
            commit('getNetWorkList', arg)
        },
    }
};

export default common
